<?php

include 'angkatan_controller.php';
include 'mahasiswa_controller.php';

if ($_GET['action'] == "account") {
    $json = $HTTP_RAW_POST_DATA;
    if ($json != NULL) {
        echo viewMahasiswaAccount($json);
    } else {
        echo 'Invalid Request';
    }
} else if ($_GET['action'] == "updateAccount") {
    $json = $HTTP_RAW_POST_DATA;
    if ($json != NULL) {
        if (updateMahasiswaAccount($json))
            echo 'Update Success.';
        else
            echo 'Update Failed!';
    } else {
        echo 'Invalid Request';
    }
} else if ($_GET['action'] == "register") {
    $json = $HTTP_RAW_POST_DATA;
    if ($json != NULL) {
        $jsArray = json_decode($json, false);
        $nim = $jsArray->{'Nim'};
        $angkatan = $jsArray->{'TahunAngkatan'}->{'Tahun'};

        $id_mhs = $angkatan . substr($nim, 2); // 2008 + substring (0804505080, 2) = 200804505080
        $id_angkatan = getAngkatanId($angkatan);

        $count = countMahasiswa($nim);
        
        if ($count > 0) {
            echo 'NIM ' . $nim . ' sudah terdaftar';
        } else {
            if (registerMahasiswa($jsArray, $id_mhs, $id_angkatan))
                echo 'Register Success. Tunggu sampai dosen anda menyetujui.';
            else
                echo 'Register Failed! Hubungi dosen anda.';
        }
    } else {
        echo 'Invalid Request';
    }
} else if ($_GET['action'] == "login") {
    $json = $HTTP_RAW_POST_DATA;
    if ($json != NULL) {
        $jsArray = json_decode($json, false);
        $nim = $jsArray->{'Nim'};
        $username = $jsArray->{'Username'};
        $password = $jsArray->{'Password'};

        echo loginMahasiswa($nim, $username, $password);
    } else {
        echo 'Invalid Request';
    }
} else if ($_GET['action'] == "viewNotApproved") {
    echo viewNotApprovedMahasiswa();
} else if ($_GET['action'] == "listMahasiswa") {
    echo listOfMahasiswa();
} else if ($_GET['action'] == "listAngkatan") {
    echo listAngkatan();
} else {
    echo 'Action not defined!';
}
?>
